package com.mnb.base.controller;

import com.mnb.base.entity.SysUser;
import com.mnb.base.service.SysUserService;
import com.mnb.common.utils.R;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/system/user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private HttpSession session;


    @RequestMapping("/pageList")
    public R pageList(SysUser sysUser) {
        return sysUserService.getPageList(sysUser);
    }

    @RequestMapping("/list")
    public R list(SysUser sysUser) {
        return sysUserService.getList(sysUser);
    }

    @RequestMapping("/insert")
    public R insert(@RequestBody SysUser sysUser) {
        return sysUserService.insert(sysUser);
    }

    @RequestMapping("/udpate")
    public R udpate(@RequestBody SysUser sysUser) {
        return sysUserService.udpate(sysUser);
    }

    @RequestMapping("/delete")
    public R delete(String[] userIds) {
        if (ArrayUtils.contains(userIds, getUserId()))
        {
            return R.ok("当前用户不能删除");
        }
        return sysUserService.deleteUserByIds(userIds);
    }

    @RequestMapping("/resetUserPwd")
    public R resetUserPwd(SysUser sysUser) {
        return sysUserService.resetUserPwd(sysUser);
    }

    @RequestMapping("/getUserRoleList")
    public R getUserRoleList(SysUser sysUser) {
        return sysUserService.getUserRoleList(sysUser);
    }

    public Object getUserId()
    {
        SysUser user = (SysUser) session.getAttribute("user");
        if(user == null){
            throw new RuntimeException("用户未登录");
        }
        return user.getUserId();
    }
}
